home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 June / ccd0605.iso / Software / Shareware / Programare / nativej / nativej-trial.exe / {app} / examples-source / Service2.java < prev    next >
Text File  |  2003-04-09  |  5KB  |  156 lines

  1. package examples;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import java.util.*;
  6.  
  7. /**
  8.  * This is a sample Java program that runs as a service.
  9.  *
  10.  * It appends the current date/time to a file called "service.log" at 5 secs interval
  11.  * until it is stopped.
  12.  *
  13.  * The "stop" command is implemented using datagram. Since IP is used to signal
  14.  * termination, it does not matter whether this is done within the same or separate JVM
  15.  * space. As such, this approach will work with BOTH command line and
  16.  * NativeJ-generated executables.
  17.  */
  18. public class Service2
  19. {
  20.     /**
  21.      * This is the port over which the termination signal is sent.
  22.      */
  23.     private final static int port = 5678;
  24.  
  25.     /**
  26.      * This is the message to be sent to signal termination.
  27.      */
  28.     private final static String terminator = "QUIT";
  29.  
  30.  
  31.     /**
  32.      * The main() function accepts one single parameter: "start" or "stop".
  33.      * The first parameter starts the date/time logging service, while the
  34.      * second parameter stops the service.
  35.      */
  36.     public static void main(String[] args) throws Exception
  37.     {
  38.         // Start the service
  39.         if (args[0].equals("start"))
  40.         {
  41.             try
  42.             {
  43.                 // Run the termination listener thread
  44.                 TerminationListener t = new TerminationListener(
  45.                     port, terminator, Thread.currentThread());
  46.                 t.start();
  47.  
  48.                 // Start the logging service
  49.                 while(true)
  50.                 {
  51.                     // Append current date/time to log file
  52.                     log("Current date/time is " + new Date());
  53.  
  54.                     // Sleep for 5 secs
  55.                     Thread.currentThread().sleep(5000);
  56.                 }
  57.             }
  58.             catch(InterruptedException e)
  59.             {
  60.                 // Exit when thread is interrupted.
  61.                 log("Service stopped.");
  62.             }
  63.         }
  64.         else
  65.         // Stop the service
  66.         if (args[0].equals("stop"))
  67.         {
  68.             try
  69.             {
  70.                 // Send the termination message
  71.                 DatagramSocket socket = new DatagramSocket();
  72.                 DatagramPacket packet = new DatagramPacket(
  73.                     terminator.getBytes(), terminator.length(),
  74.                     InetAddress.getLocalHost(), port);
  75.                 socket.send(packet);
  76.             }
  77.             catch(Exception e)
  78.             {
  79.                 e.printStackTrace();
  80.             }
  81.         }
  82.     }
  83.  
  84.     /**
  85.      * Log given string to file "service.log".
  86.      */
  87.     static void log(String msg) throws IOException
  88.     {
  89.         PrintWriter pw = new PrintWriter(new FileWriter("service.log", true));
  90.         pw.println(msg);
  91.         pw.close();
  92.     }
  93. }
  94.  
  95. /**
  96.  * This is a thread that will listen for the termination message over the
  97.  * designated port, then interrupt the parent thread.
  98.  */
  99. class TerminationListener extends Thread
  100. {
  101.     Thread parent;
  102.     int port;
  103.     String terminator;
  104.  
  105.     /**
  106.      * Set this thread to daemon mode so that if for some reason the main
  107.      * thread exists, this thread will not prevent the JVM from terminating.
  108.      */
  109.     public TerminationListener(int port, String terminator, Thread parent)
  110.     {
  111.         setDaemon(true);
  112.         this.port = port;
  113.         this.terminator = terminator;
  114.         this.parent = parent;
  115.     }
  116.  
  117.     /**
  118.      * Listen for the termination signal over the designated port.
  119.      */
  120.     public void run()
  121.     {
  122.         try
  123.         {
  124.             // Setup datagram socket
  125.             DatagramSocket socket = new DatagramSocket(port);
  126.             DatagramPacket packet = new DatagramPacket(
  127.                 new byte[terminator.length()], terminator.length());
  128.  
  129.             // Stop only when we have received the termination message
  130.             while(true)
  131.             {
  132.                 // Wait for a message
  133.                 socket.receive(packet);
  134.                 String msg = new String(packet.getData());
  135.  
  136.                 // Make sure the message is coming from the same machine.
  137.                 // This is included for additional security so that the program
  138.                 // cannot be terminated from an external machine).
  139.                 if (!packet.getAddress().equals(InetAddress.getLocalHost())) continue;
  140.  
  141.                 // Make sure the message is the termination message
  142.                 if (!msg.equals(terminator)) continue;
  143.  
  144.                 // Interrupt parent thread
  145.                 parent.interrupt();
  146.  
  147.                 // Terminate this thread
  148.                 break;
  149.             }
  150.         }
  151.         catch(Exception e)
  152.         {
  153.         }
  154.     }
  155. }
  156.